//
//  SinglePickerView.m
//  BusinessListing
//
//  Created by ZERO on 16/6/1.
//  Copyright © 2016年 com.ChengLian.company. All rights reserved.
//

#import "SinglePickerView.h"


@interface SinglePickerView()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) UIView       *lineView;
@property (nonatomic, strong, readonly) UIWindow *bgWindow;
@property (nonatomic, strong) UIView       *shadowView;// 阴影


@property (nonatomic, copy  ) NSString            *selectStr;// 选中信息
@property (nonatomic, copy  ) NSString            *selectId; // 选中id

@property (nonatomic, assign) NSInteger rowA;

@end

@implementation SinglePickerView
@synthesize bgWindow;
- (instancetype)init{
    self = [super init];
    if (self) {

        self.shadowView = [UIView new];
        self.shadowView.frame = [UIScreen mainScreen].bounds;
        self.shadowView.backgroundColor = [UIColor blackColor];
        self.shadowView.alpha = 0;
        self.shadowView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPicker)];
        [self.shadowView addGestureRecognizer:tap];
        
        self.backgroundColor = [UIColor whiteColor];
        self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 175);
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 35, self.bounds.size.width, self.bounds.size.height - 35)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_pickerView];

        
        self.lineView = [UIView new];
        self.lineView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 35);
        _lineView.backgroundColor = [UIColor whiteColor];
        
        UIView *bottoml = [UIView new];
        bottoml.frame = CGRectMake(0, 34, [UIScreen mainScreen].bounds.size.width, 1);
        bottoml.backgroundColor = [UIColor colorWithRed:231/255.0 green:232/255.0 blue:234/255.0 alpha:1];
        [self.lineView addSubview:bottoml];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(self.lineView.frame.size.width - 48, 0, 40, 35);
        btn.tintColor = [UIColor colorWithRed:0/255.0 green:135/255.0  blue:212/255.0 alpha:1.000];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        [self.lineView addSubview:btn];
        [self addSubview:self.lineView];
        
        
        }
    return self;
}
- (void)setType:(SingleListType)Type{
    if (_Type != Type) {
        _Type = Type;
    }
    // 数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TypeList" ofType:@"plist"];
    
    switch (_Type) {
        case SingleListTypeBusiness:
            self.arr = [[[[NSDictionary alloc] initWithContentsOfFile:path] mutableCopy] objectForKey:@"businessType"];
            break;
        case SingleListTypeID:
            self.arr = [[[[NSDictionary alloc] initWithContentsOfFile:path] mutableCopy] objectForKey:@"IDType"];
            break;
        case SingleListTypeCompensate:
            self.arr = [[[[NSDictionary alloc] initWithContentsOfFile:path] mutableCopy] objectForKey:@"CType"];
            break;
        case SingleListTypeInsuranceCompany:
            self.arr = [[[[NSDictionary alloc] initWithContentsOfFile:path] mutableCopy] objectForKey:@"InsuranceCompany"];
            
            break;
        case SingleListTypeAgent:
            
            break;
        default:
            break;
    }
    if (_Type == SingleListTypeID || _Type == SingleListTypeCompensate) {
        self.selectStr = [_arr firstObject][@"typeInfo"];
        self.selectId = [_arr firstObject][@"typeID"];
        // 设置默认值
        self.defaultID = self.selectId;
        self.defaultVal = self.selectStr;
    }else if(_Type == SingleListTypeInsuranceCompany){
        self.selectStr = [_arr firstObject][@"name"];
        self.selectId = [_arr firstObject][@"code"];
        self.defaultID = self.selectId;
        self.defaultVal = self.selectStr;
    }else if(_Type == SingleListTypeAgent){
        self.selectStr = [_arr firstObject][@"insurance_name"];
        self.selectId = [_arr firstObject][@"insurance_no"];
        self.defaultID = self.selectId;
        self.defaultVal = self.selectStr;
    }else{
        self.selectStr = [_arr firstObject];
    }
    [self.pickerView reloadAllComponents];
}
- (void)showInView
{
    self.frame = CGRectMake(0, self.shadowView.frame.size.height, self.frame.size.width, self.frame.size.height);
    
    
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, self.shadowView.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
        self.shadowView.alpha = 0.2;
    }];
    [self.bgWindow addSubview:self.shadowView];
    [self.bgWindow addSubview:self];
    
    [self.bgWindow makeKeyAndVisible];
    
    
}
- (void)click{
        [self cancelPicker];
}
- (void) dismissPicker{
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.frame = CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), self.frame.size.width, self.frame.size.height);
                         self.shadowView.alpha = 0;
                         
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         [self.shadowView removeFromSuperview];
                         NSMutableArray *windows = [[NSMutableArray alloc] initWithArray:[UIApplication sharedApplication].windows];
                         [windows removeObject:bgWindow];
                         bgWindow = nil;
                         
                         [windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *window, NSUInteger idx, BOOL *stop) {
                             if([window isKindOfClass:[UIWindow class]] && window.windowLevel == UIWindowLevelNormal) {
                                 [window makeKeyWindow];
                                 *stop = YES;
                             }
                         }];
                         
                     }];
    
}
- (void)cancelPicker
{
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.frame = CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), self.frame.size.width, self.frame.size.height);
                         self.shadowView.alpha = 0;
                         
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         
                          if (!self.selectStr && [_arr.firstObject isKindOfClass:[NSString class]]) {
                              self.selectStr = [_arr firstObject];
                              self.selectId = [_arr firstObject];
                          }
                         
                         if (self.delegate && [self.delegate respondsToSelector:@selector(complete:tempStr:idStr:)]) {
                             [self.delegate complete:self tempStr:self.selectStr idStr:self.selectId];
                         }
                         self.typeNum = [NSString stringWithFormat:@"%ld",(long)_rowA+1];
                         NSLog(@"%@", self.selectStr);
                         [self.shadowView removeFromSuperview];
                         NSMutableArray *windows = [[NSMutableArray alloc] initWithArray:[UIApplication sharedApplication].windows];
                         [windows removeObject:bgWindow];
                         bgWindow = nil;
                         
                         [windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *window, NSUInteger idx, BOOL *stop) {
                             if([window isKindOfClass:[UIWindow class]] && window.windowLevel == UIWindowLevelNormal) {
                                 [window makeKeyWindow];
                                 *stop = YES;
                             }
                         }];
                         
                     }];
    
}
- (UIWindow *)bgWindow{
    if (!bgWindow) {
        bgWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        bgWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        bgWindow.backgroundColor = [UIColor clearColor];
        //                bgWindow.alpha = 0.3;
        
        bgWindow.userInteractionEnabled = YES;
        
    }
    
    return bgWindow;
}

#pragma pickerview 代理方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

        return _arr.count;
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (self.Type == SingleListTypeID || _Type == SingleListTypeCompensate) {
        return _arr[row][@"typeInfo"];
    }else if(self.Type == SingleListTypeInsuranceCompany){
        return _arr[row][@"Name"];
    }else if(self.Type == SingleListTypeAgent){
        return _arr[row][@"insurance_name"];
    }
    return _arr[row];
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.Type == SingleListTypeID || _Type == SingleListTypeCompensate) {
        self.selectStr = _arr[row][@"typeInfo"];
        self.selectId = _arr[row][@"typeID"];

    }else if(self.Type == SingleListTypeInsuranceCompany){
        self.selectStr = _arr[row][@"Name"];
        self.selectId = _arr[row][@"Code"];
    }else if(self.Type == SingleListTypeAgent){
        self.selectStr = _arr[row][@"insurance_name"];
        self.selectId = _arr[row][@"insurance_no"];
    }
    else{
        self.selectStr = _arr[row];
    }
    _rowA = row;
    [_pickerView reloadAllComponents];

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    // 去掉两条线
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor
    = [UIColor clearColor];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor
    = [UIColor clearColor];
    
    UILabel *lbl = [UILabel new];
    lbl.textAlignment = 1;
    if (self.Type == SingleListTypeID || _Type == SingleListTypeCompensate) {
        lbl.text = _arr[row][@"typeInfo"];
    }else if(self.Type == SingleListTypeInsuranceCompany){
        lbl.text = _arr[row][@"Name"];
    }else if(self.Type == SingleListTypeAgent){
        lbl.text = _arr[row][@"insurance_name"];
    }else{
        lbl.text = [_arr objectAtIndex:row];
    }
    if (row == _rowA) {
        lbl.backgroundColor = [UIColor colorWithRed:231/255.0 green:232/255.0 blue:234/255.0 alpha:1];
    }
    return lbl;
}

-(NSString *)typeNum{
    if (_typeNum == nil) {
        _typeNum = @"0";
    }
    return _typeNum;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
